package edu.seu.mart.controller;
import edu.seu.mart.model.Address;

import edu.seu.mart.service.IAddressService;
import edu.seu.mart.service.IAdminService;
import edu.seu.mart.service.ISellerService;
import edu.seu.mart.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;


/**
 * Created by Administrator on 2015/3/11.
 */
@Controller
@RequestMapping("/address")
public class AdressController {
    @Resource
    private IAddressService addressService;
    @Resource
    private IAdminService adminService;
    @Resource
    private IUserService  userService;
    @Resource
    private ISellerService sellerService;

   /* *//**
     * 从用户的购物车页面点击结算，跳转到确认订单页面，同时显示该用户的地址列表
     *//*
    @RequestMapping(value = "/manageUserAddress",method= RequestMethod.GET)
    public String userAddressList(Model model, HttpSession session) {
        User user= (User)session.getAttribute("user");
       int userid=user.getId();
        model.addAttribute("userAddressList",AddressService.loadUserAdresss(userid));
        return "redirect:address/manageUserAddress";
    }
    *//**
     * 跳转到添加用户的地址页面
     *//*
    @RequestMapping(value = "/addUserAddress",method= RequestMethod.GET)
    public String addUserAddress(Model model ){
        model.addAttribute(new Address() );
        return "/address/addUserAddress";
    }*/
  /*  @RequestMapping(value = "/addUserAddress",method= RequestMethod.GET)
    public String addUserAddress(@ModelAttribute("address")Address address){
        return "/address/addUserAddress";
    }*/
   /**
     * 点击添加地址按钮
     */
    @RequestMapping(value = "/manageUserAddress",method= RequestMethod.POST)
    public String addUserAddress(@Validated Address address,BindingResult br){
        if (br.hasErrors()){
            return "/manageUserAddress";
        }
        return "/manageUserAddress";
    }
    /**
     * 修改地址列表中的地址
     */
    @RequestMapping(value = "/update/{id}",method= RequestMethod.POST)
    public String  userAddressUpdate(@PathVariable int id ,Model model){
        model.addAttribute("addressupdate",addressService.findAddressById(id));
        return "/userAddressUpdate";
    }
    @RequestMapping(value = "/userAddressUpdate",method= RequestMethod.POST)
    public String  userAddressUpdate(@Validated Address address,BindingResult br,Model model){
      addressService.update(address);
        //model.addAttribute("addressupdate",address);
        return "/manageUserAddress";
    }
    /**
     * 删除地址列表中不用的地址
     */
    @RequestMapping(value = "/delete/{id}",method= RequestMethod.POST)
    public String  userAddressDel(@PathVariable int id){
       addressService.delete(id);
        return "/manageUserAddress";
    }


}
